﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CNKI.Graduate.Framework.Ioc;

namespace CNKI.Graduate.Framework.Cache
{
    public static class CacheHelper
    {
        /// <summary>
        /// HttpRuntimeCache缓存key
        /// </summary>
        public const string HttpRuntimeCacheKey = "HttpRuntimeCacheKey";
        /// <summary>
        /// Redis缓存key
        /// </summary>
        public const string RedisCacheKey = "RedisCacheKey";

        private static readonly IEntityCache EntityCache;
        static CacheHelper()
        {
            EntityCache = ServiceLocator.GetInstance<IEntityCache>(RedisCacheKey);
        }
        /// <summary>
        /// 缓存字符串
        /// </summary>
        /// <param name="key">缓存对象键</param>
        /// <param name="entity">缓存字符串值</param>
        /// <param name="exp">过期时间</param>
        /// <returns></returns>
        public static bool Set(string key, string entity, TimeSpan exp)
        {
            return EntityCache.Set(key, entity, exp);
        }
        /// <summary>
        /// 缓存字符串
        /// </summary>
        /// <param name="key">缓存对象键</param>
        /// <param name="entity">缓存字符串值</param>
        /// <returns></returns>
        public static bool Set(string key, string entity)
        {
            return EntityCache.Set(key, entity);
        }
        /// <summary>
        /// 缓存对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">缓存对象键</param>
        /// <param name="entity">缓存对象</param>
        /// <param name="exp">过期时间</param>
        /// <returns></returns>
        public static bool Set<T>(string key, T entity, TimeSpan exp) where T : class
        {
            return EntityCache.Set(key, entity, exp);
        }
        /// <summary>
        /// 缓存对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">缓存对象键</param>
        /// <param name="entity">缓存对象</param>
        /// <returns></returns>
        public static bool Set<T>(string key, T entity) where T : class
        {
            return EntityCache.Set(key, entity);
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">缓存对象键</param>
        /// <returns></returns>
        public static string Get(string key)
        {
            return EntityCache.Get(key);
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">缓存对象键</param>
        /// <returns></returns>
        public static T Get<T>(string key) where T : class
        {
            return EntityCache.Get<T>(key);
        }
        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key">缓存对象键</param>
        /// <returns></returns>
        public static bool Remove(string key)
        {
            return EntityCache.Remove(key);
        }

    }
}
